package com.wctj.api.base.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Description:
 */
@Slf4j
public class InstanceConvertUtil {

    public static <S, T> List<T> convertList(List<S> rList, Class<T> clazz) {
        List<T> tList = new ArrayList<T>();
        if (CollectionUtils.isEmpty(rList)) {
            return tList;
        }
        T t = null;
        try {
            for (S s : rList) {
                t = clazz.newInstance();
                BeanUtils.copyProperties(s, t);
                tList.add(t);
            }
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return tList;
    }

    public static <T> T convertObject(Object object, Class<T> clazz) {
        if (ObjectUtils.isEmpty(object)) {
            return null;
        }
        T t = null;
        try {
            t = clazz.newInstance();
            BeanUtils.copyProperties(object, t);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return t;
    }
}
